<?
require 'global.php';
require 'upfile.php';

$sales_order=new DB(Tsales_order,$conn);
$sales_order_detail=new DB(Tsales_order_detail,$conn);
$address_book=new DB(Taddress_book,$conn);
$url='receipt';

// 错误集 error array
$errors=array();

// ref no
$ref_no=$ver=''; $valid=1;
if(isset($_GET["code"])) $ref_no=clean($_GET["code"]);
if(isset($_GET["ver"])) $ver=clean($_GET["ver"]);
if($ref_no == '' ) { header('Location:myorder.php'); exit; }

$action=isset($_POST["action"])?clean($_POST["action"]):'you guess'; // 数据库操作: CURD 
if($action=='update'){ // sql动作: 更新
	$arr=array();
	$tem_ref_no=$_POST['ref_no'];
	if(isset($_POST['payment_ref_no']))$arr['payment_ref_no']=$_POST['payment_ref_no'];
	if(isset($_POST['payer_id']))$arr['payer_id']=$_POST['payer_id'];
	if(isset($_POST['payer']))$arr['payer']=$_POST['payer'];

	$tem=$sales_order->get_row($tem_ref_no,'ref_no');
	if(! is_array($tem)){ $errors[]=array('message'=>WEB_ORDER_NOT_EXSIT,'type'=>'alert','url'=>'receipt.html'); } // 订单不存在
	else {
		if($tem['payment_method_id'] != 6){ $errors[]=array('message'=>WEB_ORDER_NOT_BANK_TRANSFER,'type'=>'alert','url'=>'receipt.html'); } // 非银行转账订单
		else { if($tem['order_status'] > 2){ $errors[]=array('message'=>WEB_ORDER_ALREADY_PAID,'type'=>'alert','url'=>'receipt.html'); } } // 非银行转账订单
	}
	if(count($errors)==0){ // 无错误, 继续执行
		$arr['order_status']=2;
		$arr['paid_time']=date('Y-m-d H:i:s'); // 提交时间
		if($_FILES["cpic"]["size"] > 0){
			$pic=upfile($_FILES["cpic"]);
			$arr['receipt_doc']=$pic;
			if(! is_null($tem['receipt_doc'])){
				$old_doc=ROOT.'upload/'.$tem['receipt_doc'];
			}
		}
		try{
			$sales_order->update($arr,'ref_no',$tem_ref_no);
			@unlink($old_doc);
			$message=array(
			'tip'=>WEB_RECEIPT_UPLOAD_SUCCESS,
			'links'=>array(array('name'=>'我的订单','url'=>'myorder.html'),array('name'=>'返回首页','url'=>'./')),
			'time'=>6.2,
			'url'=>'index.html',
			);
			require VIEW.'message.php';
			exit;
		}
		catch(Exception $e){
			//yell('Failed, please retry.',$url.'.php');
			$errors[]=array('message'=>WEB_UPDATE_FAILED,'type'=>'alert','url'=>'receipt.html');
		}
	}
}

// SEO 设置 seo tags
$title=WEB_BANK_RECEIPT_UPLOAD.' - '.$config['SITE_COMPANY_'.LANG];
$kwd=$title;
$dpn=$kwd;

// view 加载视图
require VIEW.$url.'.php';

// end of file
